﻿#include "AesEncipher.h"
#include "qaesencryption.h"

#include <QDebug>
#include <QByteArray>
#include <QCryptographicHash>

namespace GeneralAlgorithm {

	AESEncipher::AESEncipher(const QString& pubKey)
	{
		m_pubKey = QCryptographicHash::hash(pubKey.toUtf8(), QCryptographicHash::Md5);
		m_encryption = new QAESEncryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
	}

	AESEncipher::~AESEncipher()
	{
		delete m_encryption;
	}

	QByteArray AESEncipher::encode(const QByteArray& text)
	{
		return m_encryption->encode(text.toBase64(), m_pubKey).toBase64();
	}

	QByteArray AESEncipher::decode(const QByteArray& text)
	{
		return QByteArray::fromBase64(m_encryption->decode(QByteArray::fromBase64(text), m_pubKey));
	}

} // namespace Common
